package com.yonyou.pmclouds.milestone.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoInsertMultiProcessor;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoUpdateProcessor;
import com.yonyou.pmclouds.basecom.bp.template.MultiInsertBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleDeleteBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleUpdateBpTemplate;
import com.yonyou.pmclouds.basecom.consts.EnableStatusConst;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.milestone.entity.MilestoneVO;
import com.yonyou.pmclouds.milestone.mapper.MilestoneMapper;
import com.yonyou.pmclouds.milestone.processor.CheckCodeAndNameMultiProcessor;
import com.yonyou.pmclouds.milestone.processor.CheckCodeAndNameUpdateProcessor;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * @author th
 * @date 2019/11/6 15:19
 */
@Service(interfaceClass = IMilestoneMaintain.class, version = ApplicationConsts.APPLICATION_VERSION)
public class MilestoneMaintainImpl implements IMilestoneMaintain {
    public static final String[] UPDATE_FIELDS = {MilestoneVO.CODE, MilestoneVO.NAME};

    @Autowired
    private MilestoneMapper mapper;
    @Autowired
    private CheckCodeAndNameMultiProcessor checkCodeAndNameMultiProcessor;
    @Autowired
    private CheckCodeAndNameUpdateProcessor checkCodeAndNameUpdateProcessor;

    @Override
    public MilestoneVO[] save(MilestoneVO[] vos, List<String> deleteList) throws BusinessException {
        // 获取新增、更新的里程碑档案
        List<MilestoneVO> insertList = new ArrayList<>();
        List<MilestoneVO> updateList = new ArrayList<>();
        for (MilestoneVO vo : vos) {
            // 去掉空行
            if (StringUtils.isEmpty(vo.getCode()) && StringUtils.isEmpty(vo.getName()))
                continue;

            if (StringUtils.isEmpty(vo.getPkMilestone())) {
                vo.setPkTenant(RuntimeEnvironment.getTenantId());
                vo.setEnableStatus(EnableStatusConst.ENABLE);
                insertList.add(vo);
            } else {
                updateList.add(vo);
            }
        }

        // 新增
        if (CollectionUtils.isNotEmpty(insertList)) {
            MultiInsertBpTemplate<MilestoneVO> bp = new MultiInsertBpTemplate<>(mapper);
            bp.addBeforeProcessor(new AuditInfoInsertMultiProcessor<MilestoneVO>());
            bp.addBeforeProcessor(checkCodeAndNameMultiProcessor);
            bp.insert(insertList.toArray(new MilestoneVO[0]));
        }

        // 更新
        if (CollectionUtils.isNotEmpty(updateList)) {
            SingleUpdateBpTemplate<MilestoneVO> bp = new SingleUpdateBpTemplate<>(mapper, UPDATE_FIELDS);
            bp.addBeforeProcessor(new AuditInfoUpdateProcessor<MilestoneVO>());
            bp.addBeforeProcessor(checkCodeAndNameUpdateProcessor);
            for (MilestoneVO vo : vos) {
                bp.update(vo);
            }
        }

        // 删除
        if (CollectionUtils.isNotEmpty(deleteList)) {
            MilestoneVO[] deleteVOs = mapper.queryByIds(deleteList);
            SingleDeleteBpTemplate deleteBpTemplate = new SingleDeleteBpTemplate(mapper);
            deleteBpTemplate.addBeforeProcessor(new AuditInfoUpdateProcessor());
            if (ArrayUtils.isNotEmpty(deleteVOs)) {
                for (MilestoneVO vo : deleteVOs) {
                    deleteBpTemplate.delete(vo);
                }
            }
        }

        return vos;
    }

    @Override
    public MilestoneVO delete(String pk) throws BusinessException {
        MilestoneVO vo = mapper.getById(pk);
        SingleDeleteBpTemplate bp = new SingleDeleteBpTemplate(mapper);
        bp.addBeforeProcessor(new AuditInfoUpdateProcessor());
        bp.delete(vo);

        return vo;
    }

    @Override
    public MilestoneVO updateEnableStatus(MilestoneVO vo) throws BusinessException {
        SingleUpdateBpTemplate<MilestoneVO> bp = new SingleUpdateBpTemplate<>(mapper,
                new String[]{EnableStatusConst.ENABLE_STATUS});
        bp.addBeforeProcessor(new AuditInfoUpdateProcessor<MilestoneVO>());

        return bp.update(vo);
    }
}
